package com.zh.note.utils;

import com.zh.note.entity.JobBean;
import org.quartz.*;

/**
 *
 */
public class JobUtils {

    /**
     * 生成一个定时任务
     *
     * @param scheduler 调度器
     * @param jobBean   自己定义的任务bean
     */
    public static void createJob(Scheduler scheduler, JobBean jobBean) {

        Class<? extends Job> jobClass = null;
        JobDetail jobDetail = null;
        Trigger trigger = null;
        try {
            // 通过反射获取Job类
            jobClass = (Class<? extends Job>) Class.forName(jobBean.getJobClass());
            // 构建JobDetail对象，设置持久化、唯一标识和共享数据
            jobDetail = JobBuilder.newJob(jobClass).storeDurably() // 持久化
                    .withIdentity(jobBean.getJobName()) // 唯一标识
                    .usingJobData("count", 1) //数据初始化(共享数据)
                    .build();

            // 构建触发器，设置对应的任务、调度时间和唯一标识
            trigger = TriggerBuilder.newTrigger().forJob(jobDetail).withSchedule(CronScheduleBuilder.cronSchedule(jobBean.getCronExpression())) //时间规则cron表达式
                    .withIdentity(jobBean.getJobName() + "_trigger") // 唯一标识
                    .build();

            //调度执行任务
            scheduler.scheduleJob(jobDetail, trigger);


        } catch (ClassNotFoundException | SchedulerException e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 暂停一个定时任务
     *
     * @param scheduler 调度器
     * @param jobName   任务名称
     */

    public static void pauseJob(Scheduler scheduler, String jobName) {
        try {
            scheduler.pauseJob(JobKey.jobKey(jobName));
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 恢复一个定时任务
     *
     * @param scheduler
     * @param jobName
     */

    public static void resumeJob(Scheduler scheduler, String jobName) {

        try {
            scheduler.resumeJob(JobKey.jobKey(jobName));
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 删除一个定时任务
     *
     * @param scheduler
     * @param jobName
     */

    public static void deleteJob(Scheduler scheduler, String jobName) {
        try {
            scheduler.deleteJob(JobKey.jobKey(jobName));
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 运行一个定时任务一次
     *
     * @param scheduler
     * @param jobName
     */
    public static void runJobOnce(Scheduler scheduler, String jobName) {
        try {
            scheduler.triggerJob(JobKey.jobKey(jobName));
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 修改一个定时任务
     *
     * @param scheduler
     * @param jobBean   自己定义的任务bean
     *                  misfire补偿策略
     *                  withMisfireHandlingInstructionDoNothing 错过的不再补偿，然后正常调度
     *                  withMisfireHandlingInstructionFireAndProceed 错过的全部合并成一次，并立即补偿，然后正常调度 (默认)
     *                  withMisfireHandlingInstructionIgnoreMisfires 错过的全部立即补偿(即使任务终止时间已到达)，然后正常调度
     */
    public static void modifyJob(Scheduler scheduler, JobBean jobBean) {

        // 获取任务触发器唯一标识
        TriggerKey triggerKey = TriggerKey.triggerKey(jobBean.getJobName() + "_trigger");

        try {
            // 通过唯一标识获取旧的触发器
            CronTrigger oldTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);

            //使用新的cron表达式创建新的触发器
            String newCronExpression = jobBean.getCronExpression();
            CronTrigger newTrigger = oldTrigger.getTriggerBuilder()
                    .withSchedule(CronScheduleBuilder.cronSchedule(newCronExpression).withMisfireHandlingInstructionDoNothing())
                    .build();
            // 调度器更新任务的触发器
            scheduler.rescheduleJob(triggerKey, newTrigger);
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }


    }
}
